home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevtifs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.8 KB  |  326 lines

  1. /* Copyright (C) 1994, 1995 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevtifs.c,v 1.3.2.1 2000/11/16 01:39:55 rayjj Exp $ */
  20. /* TIFF-writing substructure */
  21. #include "stdio_.h"
  22. #include "time_.h"
  23. #include "gstypes.h"
  24. #include "gscdefs.h"
  25. #include "gdevprn.h"
  26. #include "gdevtifs.h"
  27.  
  28. /*
  29.  * Define the standard contents of a TIFF directory.
  30.  * Clients may add more items, also sorted in increasing tag order.
  31.  */
  32. typedef struct TIFF_std_directory_entries_s {
  33.     TIFF_dir_entry SubFileType;
  34.     TIFF_dir_entry ImageWidth;
  35.     TIFF_dir_entry ImageLength;
  36.     TIFF_dir_entry StripOffsets;
  37.     TIFF_dir_entry Orientation;
  38.     TIFF_dir_entry RowsPerStrip;
  39.     TIFF_dir_entry StripByteCounts;
  40.     TIFF_dir_entry XResolution;
  41.     TIFF_dir_entry YResolution;
  42.     TIFF_dir_entry PlanarConfig;
  43.     TIFF_dir_entry ResolutionUnit;
  44.     TIFF_dir_entry PageNumber;
  45.     TIFF_dir_entry Software;
  46.     TIFF_dir_entry DateTime;
  47. } TIFF_std_directory_entries;
  48.  
  49. /* Define values that follow the directory entries. */
  50. typedef struct TIFF_std_directory_values_s {
  51.     TIFF_ulong diroff;        /* offset to next directory */
  52.     TIFF_ulong xresValue[2];    /* XResolution indirect value */
  53.     TIFF_ulong yresValue[2];    /* YResolution indirect value */
  54. #define maxSoftware 40
  55.     char softwareValue[maxSoftware];    /* Software indirect value */
  56.     char dateTimeValue[20];    /* DateTime indirect value */
  57. } TIFF_std_directory_values;
  58. private const TIFF_std_directory_entries std_entries_initial =
  59. {
  60.     {TIFFTAG_SubFileType, TIFF_LONG, 1, SubFileType_page},
  61.     {TIFFTAG_ImageWidth, TIFF_LONG, 1},
  62.     {TIFFTAG_ImageLength, TIFF_LONG, 1},
  63.     {TIFFTAG_StripOffsets, TIFF_LONG, 1},
  64.     {TIFFTAG_Orientation, TIFF_SHORT, 1, Orientation_top_left},
  65.     {TIFFTAG_RowsPerStrip, TIFF_LONG, 1},
  66.     {TIFFTAG_StripByteCounts, TIFF_LONG, 1},
  67.     {TIFFTAG_XResolution, TIFF_RATIONAL | TIFF_INDIRECT, 1,
  68.      offset_of(TIFF_std_directory_values, xresValue[0])},
  69.     {TIFFTAG_YResolution, TIFF_RATIONAL | TIFF_INDIRECT, 1,
  70.      offset_of(TIFF_std_directory_values, yresValue[0])},
  71.     {TIFFTAG_PlanarConfig, TIFF_SHORT, 1, PlanarConfig_contig},
  72.     {TIFFTAG_ResolutionUnit, TIFF_SHORT, 1, ResolutionUnit_inch},
  73.     {TIFFTAG_PageNumber, TIFF_SHORT, 2},
  74.     {TIFFTAG_Software, TIFF_ASCII | TIFF_INDIRECT, 0,
  75.      offset_of(TIFF_std_directory_values, softwareValue[0])},
  76.     {TIFFTAG_DateTime, TIFF_ASCII | TIFF_INDIRECT, 20,
  77.      offset_of(TIFF_std_directory_values, dateTimeValue[0])}
  78. };
  79. private const TIFF_std_directory_values std_values_initial =
  80. {
  81.     0,
  82.     {0, 1},
  83.     {0, 1},
  84.     {0},
  85.     {0, 0}
  86. };
  87.  
  88. /* Fix up tag values on big-endian machines if necessary. */
  89. #if arch_is_big_endian
  90. private void
  91. tiff_fixup_tag(TIFF_dir_entry * dp)
  92. {
  93.     switch (dp->type) {
  94.     case TIFF_SHORT:
  95.     case TIFF_SSHORT:
  96.         /* We may have two shorts packed into a TIFF_ulong. */
  97.         dp->value = (dp->value << 16) + (dp->value >> 16);
  98.         break;
  99.     case TIFF_BYTE:
  100.     case TIFF_SBYTE:
  101.         dp->value <<= 24;
  102.         break;
  103.     }
  104. }
  105. #else
  106. #  define tiff_fixup_tag(dp) DO_NOTHING
  107. #endif
  108.  
  109. /* Begin a TIFF page. */
  110. int
  111. gdev_tiff_begin_page(gx_device_printer * pdev, gdev_tiff_state * tifs,
  112.              FILE * fp,
  113.              const TIFF_dir_entry * entries, int entry_count,
  114.              const byte * values, int value_size, long max_strip_size)
  115. {
  116.     gs_memory_t *mem = pdev->memory;
  117.     TIFF_std_directory_entries std_entries;
  118.     const TIFF_dir_entry *pse;
  119.     const TIFF_dir_entry *pce;
  120.     TIFF_dir_entry entry;
  121. #define std_entry_count\
  122.   (sizeof(TIFF_std_directory_entries) / sizeof(TIFF_dir_entry))
  123.     int nse, nce, ntags;
  124.     TIFF_std_directory_values std_values;
  125.  
  126. #define std_value_size sizeof(TIFF_std_directory_values)
  127.  
  128.     tifs->mem = mem;
  129.     if (gdev_prn_file_is_new(pdev)) {
  130.     /* This is a new file; write the TIFF header. */
  131.     static const TIFF_header hdr = {
  132. #if arch_is_big_endian
  133.         TIFF_magic_big_endian,
  134. #else
  135.         TIFF_magic_little_endian,
  136. #endif
  137.         TIFF_version_value,
  138.         sizeof(TIFF_header)
  139.     };
  140.  
  141.     fwrite((const char *)&hdr, sizeof(hdr), 1, fp);
  142.     tifs->prev_dir = 0;
  143.     } else {            /* Patch pointer to this directory from previous. */
  144.     TIFF_ulong offset = (TIFF_ulong) tifs->dir_off;
  145.  
  146.     fseek(fp, tifs->prev_dir, SEEK_SET);
  147.     fwrite((char *)&offset, sizeof(offset), 1, fp);
  148.     fseek(fp, tifs->dir_off, SEEK_SET);
  149.     }
  150.  
  151.     /* We're going to shuffle the two tag lists together. */
  152.     /* Both lists are sorted; entries in the client list */
  153.     /* replace entries with the same tag in the standard list. */
  154.     for (ntags = 0, pse = (const TIFF_dir_entry *)&std_entries_initial,
  155.      nse = std_entry_count, pce = entries, nce = entry_count;
  156.      nse && nce; ++ntags
  157.     ) {
  158.     if (pse->tag < pce->tag)
  159.         ++pse, --nse;
  160.     else if (pce->tag < pse->tag)
  161.         ++pce, --nce;
  162.     else
  163.         ++pse, --nse, ++pce, --nce;
  164.     }
  165.     ntags += nse + nce;
  166.     tifs->ntags = ntags;
  167.  
  168.     /* Write count of tags in directory. */
  169.     {
  170.     TIFF_short dircount = ntags;
  171.  
  172.     fwrite((char *)&dircount, sizeof(dircount), 1, fp);
  173.     }
  174.     tifs->dir_off = ftell(fp);
  175.  
  176.     /* Fill in standard directory tags. */
  177.     std_entries = std_entries_initial;
  178.     std_values = std_values_initial;
  179.     std_entries.ImageWidth.value = pdev->width;
  180.     std_entries.ImageLength.value = pdev->height;
  181.     if (max_strip_size == 0) {
  182.     tifs->strip_count = 1;
  183.     tifs->rows = pdev->height;
  184.     std_entries.RowsPerStrip.value = pdev->height;
  185.     } else {
  186.     int max_strip_rows =
  187.         max_strip_size / gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  188.         int rps = max(1, max_strip_rows);
  189.  
  190.     tifs->strip_count = (pdev->height + rps - 1) / rps;
  191.     tifs->rows = rps;
  192.     std_entries.RowsPerStrip.value = rps;
  193.     std_entries.StripOffsets.count = tifs->strip_count;
  194.     std_entries.StripByteCounts.count = tifs->strip_count;
  195.     }
  196.     tifs->StripOffsets = (TIFF_ulong *)gs_alloc_bytes(mem,
  197.                 (tifs->strip_count)*2*sizeof(TIFF_ulong),
  198.             "gdev_tiff_begin_page(StripOffsets)");
  199.     tifs->StripByteCounts = &(tifs->StripOffsets[tifs->strip_count]);
  200.     if (tifs->StripOffsets == 0)
  201.     return_error(gs_error_VMerror);
  202.     std_entries.PageNumber.value = (TIFF_ulong) pdev->PageCount;
  203.     std_values.xresValue[0] = pdev->x_pixels_per_inch;
  204.     std_values.yresValue[0] = pdev->y_pixels_per_inch;
  205.     {
  206.     char revs[10];
  207.  
  208.     strncpy(std_values.softwareValue, gs_product, maxSoftware);
  209.     std_values.softwareValue[maxSoftware - 1] = 0;
  210.     sprintf(revs, " %1.2f", gs_revision / 100.0);
  211.     strncat(std_values.softwareValue, revs,
  212.         maxSoftware - strlen(std_values.softwareValue) - 1);
  213.     std_entries.Software.count =
  214.         strlen(std_values.softwareValue) + 1;
  215.     }
  216.     {
  217.     struct tm tms;
  218.     time_t t;
  219.  
  220.     time(&t);
  221.     tms = *localtime(&t);
  222.     sprintf(std_values.dateTimeValue,
  223.         "%04d:%02d:%02d %02d:%02d:%02d",
  224.         tms.tm_year + 1900, tms.tm_mon, tms.tm_mday,
  225.         tms.tm_hour, tms.tm_min, tms.tm_sec);
  226.     }
  227.  
  228.     /* Write the merged directory. */
  229.     for (pse = (const TIFF_dir_entry *)&std_entries,
  230.      nse = std_entry_count, pce = entries, nce = entry_count;
  231.      nse || nce;
  232.     ) {
  233.     bool std;
  234.  
  235.     if (nce == 0 || (nse != 0 && pse->tag < pce->tag))
  236.         std = true, entry = *pse++, --nse;
  237.     else if (nse == 0 || (nce != 0 && pce->tag < pse->tag))
  238.         std = false, entry = *pce++, --nce;
  239.     else
  240.         std = false, ++pse, --nse, entry = *pce++, --nce;
  241.     if (entry.tag == TIFFTAG_StripOffsets)  {
  242.         if (tifs->strip_count > 1) {
  243.         tifs->offset_StripOffsets = tifs->dir_off +
  244.             (ntags * sizeof(TIFF_dir_entry)) + std_value_size + value_size;
  245.         entry.value = tifs->offset_StripOffsets;
  246.         } else {
  247.         tifs->offset_StripOffsets = ftell(fp) +
  248.             offset_of(TIFF_dir_entry, value);
  249.         }
  250.     }
  251.     if (entry.tag == TIFFTAG_StripByteCounts) {
  252.         if (tifs->strip_count > 1) {
  253.             tifs->offset_StripByteCounts = tifs->dir_off +
  254.             (ntags * sizeof(TIFF_dir_entry)) + std_value_size + value_size +
  255.             (sizeof(TIFF_ulong) * tifs->strip_count);
  256.             entry.value = tifs->offset_StripByteCounts;
  257.         } else {
  258.         tifs->offset_StripByteCounts = ftell(fp) +
  259.             offset_of(TIFF_dir_entry, value);
  260.         }
  261.     }
  262.     tiff_fixup_tag(&entry);    /* don't fix up indirects */
  263.     if (entry.type & TIFF_INDIRECT) {
  264.         /* Fix up the offset for an indirect value. */
  265.         entry.type -= TIFF_INDIRECT;
  266.         entry.value +=
  267.         tifs->dir_off + ntags * sizeof(TIFF_dir_entry) +
  268.         (std ? 0 : std_value_size);
  269.     }
  270.     fwrite((char *)&entry, sizeof(entry), 1, fp);
  271.     }
  272.  
  273.     /* Write the indirect values. */
  274.     fwrite((const char *)&std_values, sizeof(std_values), 1, fp);
  275.     fwrite((const char *)values, value_size, 1, fp);
  276.     /* If StripOffsets and ByteCounts are indirect, write placeholders for them. */
  277.     if (tifs->strip_count > 1) 
  278.     fwrite(tifs->StripOffsets, sizeof(TIFF_ulong), 2 * tifs->strip_count, fp);
  279.     tifs->strip_index = 0;
  280.     tifs->StripOffsets[0] = ftell(fp);    /* start of the first strip data */
  281.     return 0;
  282. }
  283.  
  284. /* End a TIFF strip. */
  285. /* Record the size of the current strip, update the    */
  286. /* start of the next strip  and bump the strip_index    */
  287. int
  288. gdev_tiff_end_strip(gdev_tiff_state * tifs, FILE * fp)
  289. {
  290.     TIFF_ulong strip_size;
  291.     TIFF_ulong next_strip_start;
  292.     int pad = 0;
  293.  
  294.     next_strip_start = (TIFF_ulong)ftell(fp);
  295.     strip_size = next_strip_start - tifs->StripOffsets[tifs->strip_index];
  296.     if (next_strip_start & 1) {
  297.         next_strip_start++;    /* WORD alignment */
  298.     fwrite(&pad, 1, 1, fp);
  299.     }
  300.     tifs->StripByteCounts[tifs->strip_index++] = strip_size;
  301.     if (tifs->strip_index < tifs->strip_count)
  302.     tifs->StripOffsets[tifs->strip_index] = next_strip_start;
  303.     return 0;
  304. }
  305.  
  306. /* End a TIFF page. */
  307. int
  308. gdev_tiff_end_page(gdev_tiff_state * tifs, FILE * fp)
  309. {
  310.     gs_memory_t *mem = tifs->mem;
  311.     long dir_off = tifs->dir_off;
  312.     int tags_size = tifs->ntags * sizeof(TIFF_dir_entry);
  313.  
  314.     tifs->prev_dir =
  315.     dir_off + tags_size + offset_of(TIFF_std_directory_values, diroff);
  316.     tifs->dir_off = ftell(fp);
  317.     /* Patch strip byte counts and offsets values. */
  318.     /* The offset in the file was determined at begin_page and may be indirect */
  319.     fseek(fp, tifs->offset_StripOffsets, SEEK_SET);
  320.     fwrite(tifs->StripOffsets, sizeof(TIFF_ulong), tifs->strip_count, fp);
  321.     fseek(fp, tifs->offset_StripByteCounts, SEEK_SET);
  322.     fwrite(tifs->StripByteCounts, sizeof(TIFF_ulong), tifs->strip_count, fp);
  323.     gs_free_object(mem, tifs->StripOffsets, "gdev_tiff_begin_page(StripOffsets)");
  324.     return 0;
  325. }
  326.